/*
Demo + Include by : Trooper[Y]
pwncurl by : DracoBlue
GeoIP by : Totto8492
*/
#if defined itranslate_inc
#endinput
#endif
#define itranslate_inc


#include <a_samp>
#include <a_http>
//#include <pwncurl>
//#include <GeoIP_Plugin>

#include <mygeoip>

#tryinclude num_players

//forward translate(playerid,string[],endstring[],fromplayer);
//forward iTranslate_OnPlayerConnect(playerid);
new cntryname[MAX_PLAYERS][4];



iTranslate_OnPlayerConnect(playerid)
{
	new ip[16];
 	GetPlayerIp(playerid, ip, 16);
 	mygeoip_code2(ip, cntryname[playerid], 4);
	printf("ID %d comes from %s",playerid,cntryname[playerid]);
 	return 1;
}


stock translate(playerid, const string[], fromplayer = -1)
{
	/*
 	if(strfind(string," ") != -1)
 	{
 	    for(new lp_form=0;lp_form<=strlen(string);lp_form++)
 	    {
 	        preppp = strfind(string," ");
 	        if(preppp == -1) break;
 	        strdel(string,preppp,preppp+1);
 	        strins(string,"+",preppp,strlen(string));
 	    }
 	}*/

	new url[255];

 	//if(IsPlayerConnected(fromplayer)) format(url, 255, "%s%s%s%s%s%s%s", "http://translate.google.de/translate_t?hl=&ie=UTF-8&text=",string,"&sl=",cntryname[fromplayer],"&tl=",cntryname[playerid],"#");
	format(url, 255, "http://translate.google.de/translate_t?hl=&ie=UTF-8&text=%s&sl=%s&tl=%s#",string,
		IsPlayerConnected(fromplayer)?cntryname[fromplayer]:"en",cntryname[playerid]);
	//pwncurl_get(url,cc_content,sizeof(cc_content));
	HTTP(playerid, HTTP_GET, url, "HTTP 1.0\r\n", "OnTranslateReceived");
	return 1;
}


forward OnTranslateReceived(index, response_code, data[]);
public OnTranslateReceived(index, response_code, data[])
{
	new preppp, preppp2;
	new endstring[128];
	printf("code: %d", response_code);
    preppp = strfind(data,"this.style.backgroundColor");
	if(preppp == -1)
	{
	    print(" - iTranslate Functional Error1 - ");
	    fremove("src.txt");
	 	new File:handd = fopen("src.txt");
	 	fwrite(handd, data);
	 	fclose(handd);
        return 0;
 	}
	strdel(data,0,preppp);
	strdel(data,0,9+76);
	preppp2 = strfind(data,"<");
	if(preppp2 == -1)
	{
	    print(" - iTranslate Functional Error2 - ");
        return 0;
 	}
	strdel(data,preppp2,strlen(data));
	//strdel(cc_content,strlen(cc_content)-1,strlen(cc_content));
	strpack(endstring,data,strlen(data));
	printf("%s", endstring);

	SendClientMessage(index, 0xffffffff, endstring);
	return 1;
}

